package com.iiitb.action;

import java.util.ArrayList;
import java.util.List;
import co.iiitb.DAO.AdminDAO;
import co.iiitb.DAO.UserDAO;
import java.util.Iterator;
import com.iiitb.model.Department;
import com.iiitb.model.Project;
import com.iiitb.model.User_Login;
import com.iiitb.utils.MyLog;
import com.opensymphony.xwork2.ActionSupport;

public class AdminFuncActions extends ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//Department Parameters
	private String deptName;
	private int deptId;
	
	//Project Parameters
	private String projectId;
	private String pcode;
	private String pname;
	private String proj_clientId;
	private String pmgrId;
	private String proj_deptId;
	private int projBudget;
	private int projManHour;
	
	//action name i.e. either to create or edit project
	private String actionName;
	//list of all departments present
	private List<Department> departments;
	//list of all users for selecting manager
	private List<User_Login>managers=new ArrayList<User_Login>();
	//list of all projects
	private List<Project> projects;
	//list of all clients
	private List<User_Login> clients=new ArrayList<User_Login>();
	
	
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public String getProjectId() {
		return projectId;
	}
	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}
	public String getPcode() {
		return pcode;
	}
	public void setPcode(String pcode) {
		this.pcode = pcode;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public String getProj_clientId() {
		return proj_clientId;
	}
	public void setProj_clientId(String proj_clientId) {
		this.proj_clientId = proj_clientId;
	}
	public String getPmgrId() {
		return pmgrId;
	}
	public void setPmgrId(String pmgrId) {
		this.pmgrId = pmgrId;
	}
	public String getProj_deptId() {
		return proj_deptId;
	}
	public void setProj_deptId(String proj_deptId) {
		this.proj_deptId = proj_deptId;
	}
	public int getProjBudget() {
		return projBudget;
	}
	public void setProjBudget(int projBudget) {
		this.projBudget = projBudget;
	}
	public int getProjManHour() {
		return projManHour;
	}
	public void setProjManHour(int projManHour) {
		this.projManHour = projManHour;
	}
	
	public List<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}
	public String getActionName() {
		return actionName;
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}
	public List<User_Login> getManagers() {
		return managers;
	}
	public void setManagers(List<User_Login> managers) {
		this.managers = managers;
	}
	public List<Project> getProjects() {
		return projects;
	}
	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}
	public List<User_Login> getClients() {
		return clients;
	}
	public void setClients(List<User_Login> clients) {
		this.clients = clients;
	}
	
	
	//method for createProject Action
	public String loadCreateProject() 
	{
		setActionName("createProject");
		departments=AdminDAO.getAllDepartments();
		List<User_Login>users;
		users=UserDAO.getAllUsers();
		for (Iterator<User_Login> it = users.iterator(); it.hasNext();) 
		{
			User_Login u = it.next();
			if (u.getRole().equalsIgnoreCase("Manager")) {

				managers.add(u);
			}
			if (u.getRole().equalsIgnoreCase("Client")) {
				clients.add(u);
			}
		}
		String maxId= "select max(project_id) from project";
		int count=AdminDAO.getMaxId(maxId);
		count=count+1;
		Integer c=new Integer(count);
		setProjectId(c.toString());
		return "showproject";
	}
	
	//insert project in database
	public String createProjectDB() 
	{
		System.out.println("action :" + actionName);
		MyLog.log("in create Project");
		int count;
		//departments = AdminDAO.getDepartments();
		if (actionName.equals("Create Project")) 
		{

				Project proj=new Project();
				proj.setProjectId(Integer.parseInt(projectId));
				proj.setProjectCode(pcode);
				proj.setProjectName(pname);
				proj.setClientId(Integer.parseInt(proj_clientId));
				proj.setDeptId(Integer.parseInt(proj_deptId));
				proj.setProjMgrId(Integer.parseInt(pmgrId));
				proj.setBudget(projBudget);
				proj.setMan_hour_req(projManHour);
				String insertSQL = "insert into project "
						+ "(project_id,pcode, name,client_id,pmgr_id,dept_id,budget,Man_hour_req) " + 
						"values("+proj.getProjectId()+ ", '" + proj.getProjectCode() + "', '" + proj.getProjectName() 
						+ "', " + proj.getClientId() +","+proj.getProjMgrId()+","+proj.getDeptId()+ "," + proj.getBudget()
						+","+ proj.getMan_hour_req()+ ");";
				count=AdminDAO.insert(insertSQL);
				if(count!=0)
				{
					addActionMessage("You have successfully created a project.");
					return "success";
				}
				else
				{
					addActionError("Error during insertion");
					return "fail";
				}
		}
		if (actionName.equals("Edit Project")) 
		{
			Project proj=new Project();
			proj.setProjectId(Integer.parseInt(projectId));
			proj.setProjectCode(pcode);
			proj.setProjectName(pname);
			proj.setClientId(Integer.parseInt(proj_clientId));
			proj.setDeptId(Integer.parseInt(proj_deptId));
			proj.setProjMgrId(Integer.parseInt(pmgrId));
			proj.setBudget(projBudget);
			proj.setMan_hour_req(projManHour);
			String updateSQL = "update project "
							+ "set pcode = '" + proj.getProjectCode() + "', name = '" + 
						    proj.getProjectName() + "', client_id = " + 
							proj.getClientId() + ", pmgr_id = " + 
						    proj.getProjMgrId() +  ", dept_id = " + 
							proj.getDeptId()+ ", budget = " + 
							proj.getBudget()+ ", Man_hour_req = " +
							proj.getMan_hour_req()+
							" where project_id = " +proj.getProjectId();
			System.out.println("printing query:"+updateSQL);
			count=AdminDAO.update(updateSQL);
			if(count!=0)
			{
				addActionMessage("You have successfully edited a project.");
				return "success";
			}
			else
			{
				addActionError("Error during updation");
				return "fail";
			}

		}
		addActionError("Error occured please try again");
		return "fail";
	}
	
	
	public String showProjects() 
	{
		MyLog.log("in show projects.");
		projects=AdminDAO.getAllProjects();
		return "show";
	}
	
	public String editProject() 
	{
		setActionName("editProject");
		System.out.println("params :" + projectId + " : " + pname + ":"
				+ proj_deptId);
		departments=AdminDAO.getAllDepartments();
		List<User_Login>users;
		users=UserDAO.getAllUsers();
		for (Iterator<User_Login> it = users.iterator(); it.hasNext();) 
		{
			User_Login u = it.next();
			if (u.getRole().equalsIgnoreCase("Manager")) {

				managers.add(u);
			}
			if (u.getRole().equalsIgnoreCase("Client")) {
				clients.add(u);
			}
		}
		return "showproject";
	}
	
	//function to set action for create department
	public String loadCreateDepartment() 
	{
		setActionName("createDepartment");
		String maxId= "select max(dept_id) from department";
		int count=AdminDAO.getMaxId(maxId);
		count=count+1;
		//Integer c=new Integer(count);
		setDeptId(count);
		return "loadDepartment";
	}
	
	public String createDepartmentDB()
	{
		int count;
		MyLog.log("in create department of Admin action");
		if(actionName.equalsIgnoreCase("Create Department"))
		{
			Department dept=new Department();
			dept.setDeptId(deptId);
			dept.setDeptName(deptName);
			String insertSQL = "insert into department "
					+ "(dept_id,name) " + 
					"values("+dept.getDeptId()+ ", '" + dept.getDeptName()+ "'"
					+ ");";
			count=AdminDAO.insert(insertSQL);
			if(count!=0)
			{
				addActionMessage("You have successfully created a Department.");
				return "success";
			}
			else
			{
				addActionError("Error during insertion of Department.");
				return "fail";
			}
		}
		if(actionName.equalsIgnoreCase("Edit Department"))
		{
			System.out.println("in edit");
			Department dept=new Department();
			dept.setDeptId(deptId);
			dept.setDeptName(deptName);
			String updateSQL = "update department "
					+ "set name = '" + dept.getDeptName() + "'" + 
					" where dept_id = " +dept.getDeptId();
			System.out.println("printing query:"+updateSQL);
			count=AdminDAO.update(updateSQL);
			if(count!=0)
			{
				addActionMessage("You have successfully updated a department.");
				return "success";
			}
			else
			{
				addActionError("Error during updation of department.");
				return "fail";
			}

		}
		System.out.println("didnt went in create or edit");
		addActionError("Error occured please try again");
		return "fail";
	}
	
	public String editDeparment() 
	{
		setActionName("editDepartment");
		MyLog.log("in edit department action method");
		return "showDept";
	}
	
	public String showDepartment() 
	{
		MyLog.log("in show department");
		departments=AdminDAO.getAllDepartments();
		return "show";
	}
}
